package com.njtoyo.taxi.admin.library.qiniu;

import com.qiniu.util.Auth;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@ConfigurationProperties(prefix = "config.qiniu")
@Data
public class QiNiuService {
    private String accessKey;
    private String secretKey;
    private Map<String, BucketConfig> buckets;

    @Data
    public static class BucketConfig {
        private String bucket;
        private String token;
        private String domain;
    }

    public Auth auth() {
        return Auth.create(this.accessKey, this.secretKey);
    }

    public BucketConfig uploadToken(String bucket) {
        BucketConfig res = new BucketConfig();
        res.setToken(auth().uploadToken(this.getBuckets().get(bucket).getBucket()));
        res.setDomain(this.getBuckets().get(bucket).getDomain());
        return res;
    }
}
